import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules */

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '控制台', icon: 'dashboard', affix: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/permission',
    component: Layout,
    alwaysShow: true, // will always show the root menu
    name: '系统管理',
    meta: {
      title: '权限管理',
      icon: 'lock',
      roles: ['admin', 'manager'] // you can set roles in root nav
    },
    children: [
      {
        path: 'config',
        component: () => import('@/views/config/index'),
        name: 'Config',
        meta: {
          title: '系统配置',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'admin',
        component: () => import('@/views/admin/user'),
        name: 'PermissionAdmin',
        meta: {
          title: '管理员管理',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    alwaysShow: true, // will always show the root menu
    name: '用户管理',
    meta: {
      title: '用户管理',
      icon: 'peoples',
      roles: ['admin', 'manager'] // you can set roles in root nav
    },
    children: [
      {
        path: 'list',
        component: () => import('@/views/user/list'),
        name: 'User',
        meta: {
          title: '用户管理',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'firm',
        component: () => import('@/views/firm/list'),
        name: 'User',
        meta: {
          title: '家族长管理',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      }
    ]
  },
  {
    path: '/audit',
    component: Layout,
    alwaysShow: true, // will always show the root menu
    name: '审核管理',
    meta: {
      title: '审核管理',
      icon: 'skill',
      roles: ['admin', 'manager'] // you can set roles in root nav
    },
    children: [
      {
        path: 'post',
        component: () => import('@/views/post/list'),
        name: 'Post',
        meta: {
          title: '动态审核',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'postComment',
        component: () => import('@/views/post/comment'),
        name: 'PostComment',
        meta: {
          title: '动态评论审核',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'lantern',
        component: () => import('@/views/lantern/list'),
        name: 'lantern',
        meta: {
          title: '孔明灯审核',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'lanternComment',
        component: () => import('@/views/lantern/comment'),
        name: 'lanternComment',
        meta: {
          title: '孔明灯评论审核',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'photoAlbum',
        component: () => import('@/views/photoAlbum/list'),
        name: 'photoAlbum',
        meta: {
          title: '相册审核',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'avatar',
        component: () => import('@/views/headImg/list'),
        name: 'avatar',
        meta: {
          title: '头像审核',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'nicknameSignature',
        component: () => import('@/views/nicknameSignature/list'),
        name: 'nicknameSignature',
        meta: {
          title: '昵称签名审核',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'voiceBusinessCards',
        component: () => import('@/views/audio/list'),
        name: 'voiceBusinessCards',
        meta: {
          title: '语音名片审核',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'voiceMachineAudits',
        component: () => import('@/views/voiceMachineAudit/list'),
        name: 'voiceMachineAudits',
        meta: {
          title: '语音机审记录',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      }
    ]
  },
  {
    path: '/order',
    component: Layout,
    alwaysShow: true, // will always show the root menu
    name: '财务管理',
    meta: {
      title: '财务管理',
      icon: 'money',
      roles: ['admin', 'manager'] // you can set roles in root nav
    },
    children: [
      {
        path: 'order',
        component: () => import('@/views/order/list'),
        name: 'order',
        meta: {
          title: '充值订单',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'withdraw',
        // component: () => import('@/views/user/list'),
        name: '提现管理',
        meta: {
          title: '提现管理',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'coins',
        component: () => import('@/views/coinsLog/list'),
        name: '金币流水',
        meta: {
          title: '金币流水',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'money',
        component: () => import('@/views/moneyLog/list'),
        name: '余额流水',
        meta: {
          title: '余额流水',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      }
    ]
  },
  {
    path: '/operation',
    component: Layout,
    alwaysShow: true, // will always show the root menu
    name: '运营管理',
    meta: {
      title: '运营管理',
      icon: 'education',
      roles: ['admin', 'manager'] // you can set roles in root nav
    },
    children: [
      {
        path: 'coins',
        component: () => import('@/views/operation/coins/index'),
        name: '金币套餐管理',
        meta: {
          title: '金币套餐管理',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'keywords',
        component: () => import('@/views/operation/keyword/index'),
        name: '关键字管理',
        meta: {
          title: '关键字管理',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'backgroundMusic',
        // component: () => import('@/views/user/list'),
        name: '背景音乐管理',
        meta: {
          title: '背景音乐管理',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'ad',
        // component: () => import('@/views/user/list'),
        name: '广告管理',
        meta: {
          title: '广告管理',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'gift',
        component: () => import('@/views/operation/gift/index'),
        name: '礼物管理',
        meta: {
          title: '礼物管理',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'postCategory',
        component: () => import('@/views/operation/postCategory/index'),
        name: '动态分类管理',
        meta: {
          title: '动态分类管理',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'voiceChatCategory',
        // component: () => import('@/views/user/list'),
        name: '语聊分类管理',
        meta: {
          title: '语聊分类管理',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'post',
        // component: () => import('@/views/user/list'),
        name: '动态管理',
        meta: {
          title: '动态管理',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'anchor',
        component: () => import('@/views/anchor/list'),
        name: '主播申请管理',
        meta: {
          title: '主播申请',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'task',
        component: () => import('@/views/operation/task/index'),
        name: '每日任务管理',
        meta: {
          title: '每日任务管理',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      }
    ]
  },
  {
    path: '/voice',
    component: Layout,
    alwaysShow: true, // will always show the root menu
    name: '语音管理',
    meta: {
      title: '语音管理',
      icon: 'wechat',
      roles: ['admin', 'manager'] // you can set roles in root nav
    },
    children: [
      {
        path: 'room',
        // component: () => import('@/views/user/list'),
        name: '语聊房间管理',
        meta: {
          title: '语聊房间管理',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'call',
        // component: () => import('@/views/user/list'),
        name: '语聊通话管理',
        meta: {
          title: '语聊通话管理',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      }
    ]
  },
  {
    path: '/feedback',
    component: Layout,
    alwaysShow: true, // will always show the root menu
    name: '反馈管理',
    meta: {
      title: '反馈管理',
      icon: 'email',
      roles: ['admin', 'manager'] // you can set roles in root nav
    },
    children: [
      {
        path: 'tickets',
        // component: () => import('@/views/user/list'),
        name: '工单列表',
        meta: {
          title: '工单列表',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'feedback',
        // component: () => import('@/views/user/list'),
        name: '举报反馈',
        meta: {
          title: '举报反馈',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'report',
        // component: () => import('@/views/user/list'),
        name: '举报管理',
        meta: {
          title: '举报反馈',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'send',
        // component: () => import('@/views/user/list'),
        name: '发送通知',
        meta: {
          title: '举报反馈',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      }
    ]
  },
  {
    path: '/message',
    component: Layout,
    alwaysShow: true, // will always show the root menu
    name: '消息监控',
    meta: {
      title: '消息监控',
      icon: 'message',
      roles: ['admin', 'manager'] // you can set roles in root nav
    },
    children: [
      {
        path: 'feedback',
        // component: () => import('@/views/user/list'),
        name: '实时消息',
        meta: {
          title: '实时消息',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'call',
        component: () => import('@/views/chat/list'),
        name: '聊天记录',
        meta: {
          title: '聊天记录',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      }
    ]
  },
  {
    path: '/version',
    component: Layout,
    alwaysShow: true, // will always show the root menu
    name: '版本控制',
    meta: {
      title: '版本控制',
      icon: 'bug',
      roles: ['admin', 'manager'] // you can set roles in root nav
    },
    children: [
      {
        path: 'control',
        // component: () => import('@/views/user/list'),
        name: '版本控制',
        meta: {
          title: '版本控制',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      }
    ]
  },
  {
    path: '/fake',
    component: Layout,
    alwaysShow: true, // will always show the root menu
    name: '假用户管理',
    meta: {
      title: '假用户管理',
      icon: 'user',
      roles: ['admin', 'manager'] // you can set roles in root nav
    },
    children: [
      {
        path: 'user',
        component: () => import('@/views/fake/user/index'),
        name: '假用户',
        meta: {
          title: '假用户',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'category',
        component: () => import('@/views/fake/category/index'),
        name: '假用户类型',
        meta: {
          title: '假用户类型',
          roles: ['admin', 'manager'] // or you can only set roles in sub nav
        }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
